<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:DentalProcessorControls="clr-namespace:DentalProcessorControls;assembly=DentalProcessorControls"
  xmlns:DentalHelperLib="clr-namespace:DentalHelperLib;assembly=DentalHelperLib"
  xmlns:converters="clr-namespace:DentalConfig.Converters;assembly=DentalConfig"
  xmlns:materialConverters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
  xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
  xmlns:mvvm="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45"
  xmlns:DentalGUIHelper="clr-namespace:DentalGUIHelper;assembly=DentalConfig">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="../../Common/MaterialControlStyles/MaterialDesignTheme.Shadows.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <DentalProcessorControls:BackgroundConverter x:Key="backgroundConverter"/>
  <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
  <DentalGUIHelper:InvertedBooleanToVisibilityConverter x:Key="booleanToVisibilityConverterInverted"/>
  <DentalGUIHelper:PrecentageConverter x:Key="PrecentageConverter"/>
  <materialConverters:MathConverter x:Key="MathSubtractConverter" Operation="Subtract"/>
  <materialConverters:MathMultipleConverter x:Key="MathMlpMultipleConverter" Operation="Multiply"/>
  <materialConverters:BrushRoundConverter x:Key="BrushRoundConverter"/>

  <Style x:Key="GroupBoxTreeViewSlider" TargetType="{x:Type Slider}">
    <Setter Property="Margin" Value="-22, -4, -6, -4"/>
    <Setter Property="Width" Value="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Border}, Mode=FindAncestor, AncestorLevel=2}, Converter={StaticResource MathSubtractConverter}, ConverterParameter=1.0}"/>
    <Setter Property="Height" Value="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}, Mode=FindAncestor}}"/>
    <Setter Property="Foreground" Value="{DynamicResource MaterialDesignDarkForeground}"/>
    <Setter Property="Background" Value="{DynamicResource MaterialDesignDarkBackground}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Slider}">
          <ControlTemplate.Resources>
            <Style TargetType="{x:Type RepeatButton}">
              <Setter Property="OverridesDefaultStyle" Value="true"/>
              <Setter Property="Background" Value="Transparent"/>
              <Setter Property="Focusable" Value="false"/>
              <Setter Property="IsTabStop" Value="false"/>
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Canvas Background="Transparent" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
                      <Rectangle x:Name="PART_SelectionRange" Fill="{TemplateBinding Background}" Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}"/>
                    </Canvas>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </ControlTemplate.Resources>
          <Grid
            Width="{TemplateBinding Width}"
            Height="{TemplateBinding Height}">
            <Border Name="TrackBackground" Background="Transparent"/>
            <Track x:Name="PART_Track" Margin="-4,0">
              <Track.DecreaseRepeatButton>
                <RepeatButton Command="{x:Static Slider.DecreaseLarge}"/>
              </Track.DecreaseRepeatButton>
              <Track.IncreaseRepeatButton>
                <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Background="{TemplateBinding Background}" Opacity="0.5" Margin="0,0,-4,0"/>
              </Track.IncreaseRepeatButton>
              <Track.Thumb>
                <Thumb x:Name="Thumb">
                  <Thumb.Style>
                    <Style TargetType="Thumb">
                      <Setter Property="SnapsToDevicePixels" Value="true"/>
                      <Setter Property="IsHitTestVisible" Value="false"/>
                      <Setter Property="Template">
                        <Setter.Value>
                          <ControlTemplate TargetType="Thumb">
                            <Grid Margin="-24, 0">
                              <Rectangle Width="40" Fill="Transparent" Opacity="1"/>
                              <Rectangle Width="4" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Slider}, Mode=FindAncestor}}" Opacity="1"/>
                            </Grid>
                          </ControlTemplate>
                        </Setter.Value>
                      </Setter>
                      <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                          <Setter Property="Cursor" Value="Hand"/>
                        </Trigger>
                      </Style.Triggers>
                    </Style>
                  </Thumb.Style>
                </Thumb>
              </Track.Thumb>
            </Track>
            <TextBlock
              Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Converter={StaticResource PrecentageConverter}, StringFormat={}{0}%}"
              Foreground="{TemplateBinding Foreground}"
              HorizontalAlignment="Right"
              VerticalAlignment="Center"
              Margin="8,0"
              IsHitTestVisible="False"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="GroupBoxTreeViewItem" TargetType="{x:Type TreeViewItem}">
    <Style.Resources>
      <Style TargetType="{x:Type Slider}" BasedOn="{StaticResource GroupBoxTreeViewSlider}"/>
    </Style.Resources>
    <Setter Property="FocusVisualStyle" Value="{DynamicResource TreeViewItemFocusVisual}"/>
    <Setter Property="Visibility" Value="{Binding IsHiddenFromTreeView, Converter={StaticResource booleanToVisibilityConverterInverted}}"/>
    <Setter Property="IsExpanded" Value="True"/>
    <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}"/>
    <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True"/>
    <Setter Property="DentalGUIHelper:VirtualToggleButton.IsVirtualToggleButton" Value="True"/>
    <Setter Property="DentalGUIHelper:VirtualToggleButton.IsChecked" Value="{Binding IsChecked}"/>
    <Setter Property="Background" Value="{Binding ., Converter={StaticResource backgroundConverter}}"/>
    <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, FallbackValue=#FFFFFF}"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="MinHeight" Value="{Binding Path=ElementMinHeight, Mode=OneWay}"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TreeViewItem}">
          <ControlTemplate.Resources>
            <converters:TreeViewMarginConverter Length="12" x:Key="LengthConverter"/>
          </ControlTemplate.Resources>
          <StackPanel>
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="ExpansionStates">
              <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0" To="Expanded">
                  <VisualTransition.GeneratedEasingFunction>
                    <CubicEase EasingMode="EaseOut"/>
                  </VisualTransition.GeneratedEasingFunction>
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ItemsHost" To="1" Duration="0:0:0.3"/>
                    <DoubleAnimation Storyboard.TargetProperty="Scale" Storyboard.TargetName="ScaleHost" To="1" Duration="0:0:0.3"/>
                  </Storyboard>
                </VisualTransition>
                <VisualTransition GeneratedDuration="0" To="Collapsed">
                  <VisualTransition.GeneratedEasingFunction>
                    <CubicEase EasingMode="EaseOut"/>
                  </VisualTransition.GeneratedEasingFunction>
                  <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Visibility">
                      <DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{x:Static Visibility.Collapsed}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ItemsHost" To="0" Duration="0:0:0.3"/>
                    <DoubleAnimation Storyboard.TargetProperty="Scale" Storyboard.TargetName="ScaleHost" To="0" Duration="0:0:0.3"/>
                  </Storyboard>
                </VisualTransition>
              </VisualStateGroup.Transitions>
              <VisualState x:Name="Expanded">
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ItemsHost" To="1" Duration="0"/>
                  <DoubleAnimation Storyboard.TargetProperty="Scale" Storyboard.TargetName="ScaleHost" To="1" Duration="0:0:0.3"/>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Collapsed">
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ItemsHost" To="0" Duration="0"/>
                  <DoubleAnimation Storyboard.TargetProperty="Scale" Storyboard.TargetName="ScaleHost" To="0" Duration="0:0:0.3"/>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemsHost" Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
            <Border
              x:Name="Border"
              SnapsToDevicePixels="true"
              IsManipulationEnabled="True"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              Padding="{TemplateBinding Padding}"
              MinHeight="{TemplateBinding MinHeight}">
              <Grid VerticalAlignment="Center" IsManipulationEnabled="True">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="22"/>
                  <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition/>
                </Grid.RowDefinitions>
                <wpf:ScaleHost x:Name="ScaleHost"/>
                <Border SnapsToDevicePixels="true" x:Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                  <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MinHeight="{TemplateBinding MinHeight}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0,4,4,4"/>
                </Border>
                <ToggleButton
                  x:Name="Expander"
                  Style="{DynamicResource MaterialDesignExpandCollapseToggleStyle}"
                  IsChecked="{Binding Path=IsExpanded, RelativeSource={x:Static RelativeSource.TemplatedParent}, Mode=TwoWay}"
                  ClickMode="Press"
                  Foreground="{DynamicResource MaterialDesignDarkForeground}"/>
                <StackPanel Grid.Column="1" Grid.Row="1" x:Name="ItemsPanel">
                  <StackPanel.Height>
                    <MultiBinding Converter="{StaticResource MathMlpMultipleConverter}">
                      <Binding ElementName="ItemsHost" Path="ActualHeight"/>
                      <Binding ElementName="ScaleHost" Path="Scale"/>
                    </MultiBinding>
                  </StackPanel.Height>
                  <ItemsPresenter x:Name="ItemsHost" VerticalAlignment="Top" Opacity="0" Visibility="Collapsed"/>
                </StackPanel>
              </Grid>
            </Border>
          </StackPanel>
          <ControlTemplate.Triggers>
            <!--<Trigger Property="IsExpanded" Value="False"> <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/> </Trigger>-->
            <Trigger Property="HasItems" Value="False">
              <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
            </Trigger>
            <DataTrigger Binding="{Binding HasVisibleChildrenInTreeView}" Value="False">
              <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <Trigger Property="IsEnabled" Value="False">
              <Setter Property="Foreground" Value="{DynamicResource GrayBrush7}"/>
            </Trigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsEnabled" Value="False"/>
                <Condition Property="IsSelected" Value="True"/>
              </MultiTrigger.Conditions>
              <MultiTrigger.Setters>
                <Setter TargetName="Border" Property="Background" Value="{DynamicResource GrayBrush7}"/>
                <Setter Property="Foreground" Value="{DynamicResource WhiteBrush}"/>
              </MultiTrigger.Setters>
            </MultiTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="{Binding ., Converter={StaticResource backgroundConverter}, ConverterParameter=doLighten}"/>
      </Trigger>
      <Trigger Property="VirtualizingPanel.IsVirtualizing" Value="true">
        <Setter Property="ItemsPanel">
          <Setter.Value>
            <ItemsPanelTemplate>
              <VirtualizingStackPanel/>
            </ItemsPanelTemplate>
          </Setter.Value>
        </Setter>
      </Trigger>
    </Style.Triggers>
  </Style>
</ResourceDictionary>
